package org.luckyjourney.controller;

import org.luckyjourney.config.LocalCache;
import org.luckyjourney.config.QiNiuConfig;
import org.luckyjourney.entity.File;
import org.luckyjourney.entity.Setting;
import org.luckyjourney.holder.UserHolder;
import org.luckyjourney.service.FileService;
import org.luckyjourney.service.SettingService;
import org.luckyjourney.util.R;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @description:
 * @Author: Fxy
 * @CreateTime: 2023-11-27 20:47
 */
@RestController
@RequestMapping("/luckyjourney/file")
public class FileController implements InitializingBean {


    @Autowired
    FileService fileService;

    @Autowired
    QiNiuConfig qiNiuConfig;

    @Autowired
    SettingService settingService;

    /**
     * 保存到文件表
     *
     * @return
     */
    @PostMapping
    public R save(String fileKey) {
        return R.ok().data(fileService.save(fileKey, UserHolder.get()));
    }

    //七牛云上传凭证
    @GetMapping("/getToken")
    public R token(String type) {
        return R.ok().data(qiNiuConfig.uploadToken(type));
    }

    @GetMapping("/{fileId}")
    public void getUUid(HttpServletRequest request, HttpServletResponse response, @PathVariable Long fileId) throws IOException {
//
//        String ip = request.getHeader("referer");
//        if (!LocalCache.containsKey(ip)) {
//            response.sendError(HttpServletResponse.SC_FORBIDDEN);
//            return;
//        }
        // 如果不是指定ip调用的该接口，则不返回
        File url = fileService.getFileTrustUrl(fileId);
        response.setContentType(url.getType());
        response.sendRedirect(url.getFileKey());
    }

    //回调鉴权服务
    @PostMapping("/auth")
    public void auth(@RequestParam(required = false) String uuid, HttpServletResponse response) throws IOException {
        if (uuid == null || LocalCache.containsKey(uuid) == null) {
            response.sendError(401);
        } else {
            LocalCache.rem(uuid);
            response.sendError(200);
        }
    }

    /**
     * ，在 bean 的属性设置完成后，Spring 容器会自动调用这个方法来执行一些初始化逻辑。
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        final Setting setting = settingService.list(null).get(0);
        for (String s : setting.getAllowIp().split(",")) {
            LocalCache.put(s, true);
        }
    }
}
